﻿#region Using Directives

using System;

#endregion Using Directives

namespace SharePointStu.CAMLHelper.CAML
{
    /// <summary>
    /// Base class that defines a single element for a CAML query.
    /// </summary>
    public abstract class BaseElement
    {
        #region Private Members

        const string CAML = "<FieldRef {0}='{1}' />";

        string _field;
        Types.FieldRefTypes _fieldRefType;

        #endregion Private Members

        #region Public Properties

        /// <summary>
        /// Get or set the name of the SharePoint field to include in the result data.
        /// </summary>
        public string Field
        {
            get { return _field; }
            set { _field = value; }
        }

        /// <summary>
        /// Get or set the field reference type.
        /// </summary>
        public Types.FieldRefTypes FieldRefType
        {
            get { return _fieldRefType; }
            set { _fieldRefType = value; }
        }

        #endregion Public Properties

        #region Initialiation

        /// <summary>
        /// Base constructor for the <see cref="T:SharePointStu.CAMLHelper.CAML.BaseElement"/> object.
        /// </summary>
        /// <param name="fieldValue">The value of the field identifier</param>
        /// <param name="fieldRefType">The <see cref="T:SharePointStu.CAMLHelper.CAML.Types.FieldRefTypes"/> object type of the field to be viewed.</param>
        protected BaseElement(string fieldValue, Types.FieldRefTypes fieldRefType)
        {
            Field = fieldValue;
            FieldRefType = fieldRefType;
        }

        #endregion Initialiation

        #region Public Methods

        /// <summary>
        /// Get the CAML code for the individual element.
        /// </summary>
        /// <returns>CAML string for the individual element.</returns>
        public virtual string GetCAML()
        {
            string caml = string.Empty;

            if (Validate())
            {
                caml = string.Format(CAML, this.FieldRefType.ToString(), this.Field);
            }

            return caml;
        }

        #endregion Public Methods

        #region Protected Methods

        /// <summary>
        /// Validate the values of the <see cref="T:SharePointStu.CAMLHelper.CAML.BaseElement"/> object.
        /// </summary>
        /// <returns>bool: if the <see cref="T:SharePointStu.CAMLHelper.CAML.BaseElement"/> object values are valid.</returns>
        protected virtual bool Validate()
        {
            bool result = true;

            if (this.FieldRefType == Types.FieldRefTypes.ID)
            {
                // Ensure the field value is a valid Guid
                Guid id;
                if (!General.Helper.IsGuid(this.Field, out id))
                    throw new InvalidGuidForFieldException(this.Field);
            }

            if (string.IsNullOrEmpty(this.Field))
            {
                result = false;
            }

            return result;
        }

        #endregion Protected Methods
    }
}
